//
//  ViewController.swift
//  WriteToLocal
//
//  Created by 郑东喜 on 2017/2/24.
//  Copyright © 2017年 郑东喜. All rights reserved.
//  写入文件操作

import UIKit

class ViewController: UIViewController {
    
    // MARK:- 解压
    fileprivate lazy var zipTool: SSZipArchive = {
        let tooL : SSZipArchive = SSZipArchive()
        return tooL
    }()
    
    // MARK:- 提示框
    
    lazy var succalVC: UIAlertController = {
        let d : UIAlertController = UIAlertController.init(title: "提示", message: "写入配置文件成功", preferredStyle: .alert)
        
        d.addAction(UIAlertAction.init(title: "好的", style: .default, handler: nil))
        return d
    }()
    
    //uitabbar图标位置
    fileprivate let dicxxx : NSDictionary = ["barTitleTop" : 5,"barTitleBottom" : -5,"centerShowWIthRound" : true,"tarbarBgName" : "bhColor"]
    
    //图片数组
    fileprivate let dic : NSMutableArray = [["resultmg" : 0],
                                          ["id_1_normal" : "nav_1",
                                            "id_2_normal" : "nav_2",
                                            "id_3_normal" : "nav_3",
                                            "id_4_normal" : "nav_4",
                                            "id_5_normal" : "nav_5",
                                            "id_1_selected" : "nav_1_on",
                                            "id_2_selected" : "nav_2_on",
                                            "id_3_selected" : "nav_3_on",
                                            "id_4_selected" : "nav_4_on",
                                            "id_5_selected" : "nav_5_on"]]



    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        view.backgroundColor = UIColor.randomColor()
        
        DispatchQueue.main.async {
            self.writeFileToLocal()
        }

        
    }
}



extension ViewController {
    
    //文件写入本地
    fileprivate func writeFileToLocal() -> Void {

        ///下载文件并写入本地
        do {

            //1.获取网络资源，并写入本地
            let _data : NSData = try NSData.init(contentsOf: URL.init(string: "http://img.lingimg.com/att.php?id=0102914695")!)
        
            let filePath:String = NSHomeDirectory() + "/Documents/config.zip"
        
            //查询该文件路径的权限
            FileManageTool.fileAuthStatus(filePath: filePath)
            
            print("\((#file as NSString).lastPathComponent):(\(#line))\n",filePath)
            
            _data.write(toFile: filePath, atomically: true)
            
            
            let myDirectory:String = NSHomeDirectory() + "/Documents/bj"
            let fileManager = FileManager.default
            
            //withIntermediateDirectories为ture表示路径中间如果有不存在的文件夹都会创建
            try! fileManager.createDirectory(atPath: myDirectory,
                                             withIntermediateDirectories: true, attributes: nil)

            
            print("\((#file as NSString).lastPathComponent):(\(#line))\n",FileExist.fileIsExist(filePathStr: NSHomeDirectory() + "/Documents/bj"))
            
            //写入配置的文件
            self.writeConfigToLocal()
            
            //查询该文件路径的权限
            FileManageTool.fileAuthStatus(filePath: myDirectory)

            
            //withIntermediateDirectories为ture表示路径中间如果有不存在的文件夹都会创建
            try! fileManager.createDirectory(atPath: myDirectory,
                                             withIntermediateDirectories: true, attributes: nil)
            //解除权限
            try! FileManager.default.setAttributes([FileAttributeKey.protectionKey: FileProtectionType.none], ofItemAtPath: myDirectory)

            //解压缩
            SSZipArchive.unzipFile(atPath: filePath, toDestination: myDirectory)
            
            //删除压缩文件
            let homeDirectory = NSHomeDirectory()
            let srcUrl = homeDirectory + "/Documents/config.zip"
            try! fileManager.removeItem(atPath: srcUrl)
            

            self.present(succalVC, animated: true, completion: nil)
            
        } catch  {
            
        }
    }
}

extension ViewController {
    
    /// 写入配置文件
    fileprivate func writeConfigToLocal() -> Void {
        //（1）首先我们获取用户文档目录路径

        
        ///写入图标配置文件
        let filePath:String = NSHomeDirectory() + "/Documents/bj/tabbarIcon.plist"
        dic.write(toFile: filePath, atomically: true)
        
        //写入tabbar文字大小（x、y轴配置文件）
        let tabbarFilePath:String = NSHomeDirectory() + "/Documents/bj/tabbar.plist"
        dicxxx.write(toFile: tabbarFilePath, atomically: true)
        
    }
}


